/**
 * 
 */
package com.ywm8.ywm.admin.action;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.util.ServletContextAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.ywm8.ywm.admin.service.AdminService;
import com.ywm8.ywm.admin.service.ProvinceService;
import com.ywm8.ywm.admin.service.UnivsService;
import com.ywm8.ywm.domain.Admin;
import com.ywm8.ywm.domain.Provinces;
import com.ywm8.ywm.domain.Univs;
import com.ywm8.ywm.utils.MD5;
import com.ywm8.ywm.utils.ValidateUtil;

/**
 * @author wangruishun
 * @version ywm8 1.0 2014-1-8 下午1:29:28
 */
@Controller("adminAction")
@Scope("prototype")
public class AdminAction extends ActionSupport implements ServletContextAware {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2589439651972868156L;
	private Integer pid;
	private File picture;
	private String pictureFileName;
	private Integer uid;
	private String email;
	private ServletContext sc;
	// 店铺图片

	private Admin ad;
	private List<Univs> Ulist;
	private String code;
	private String msg;
	@Resource
	private ProvinceService ps;
	@Resource
	private UnivsService us;
	@Resource
	private AdminService as;
	private HttpServletRequest request = ServletActionContext.getRequest();

	public String getCode() {
		return code;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Integer getUid() {
		return uid;
	}

	public void setUid(Integer uid) {
		this.uid = uid;
	}
	
	/**
	 * @return the msg
	 */
	public String getMsg() {
		return msg;
	}

	/**
	 * @param msg the msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Admin getAd() {
		return ad;
	}
	
	public void setAd(Admin ad) {
		this.ad = ad;
	}

	public File getPicture() {
		return picture;
	}

	public void setPicture(File picture) {
		this.picture = picture;
	}

	public String getPictureFileName() {
		return pictureFileName;
	}

	public void setPictureFileName(String pictureFileName) {
		this.pictureFileName = pictureFileName;
	}

	public Integer getPid() {
		return pid;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}

	public List<Univs> getUlist() {
		return Ulist;
	}

	public void setUlist(List<Univs> ulist) {
		Ulist = ulist;
	}

	private List<Provinces> list;

	public List<Provinces> getList() {
		return list;
	}

	public void setList(List<Provinces> list) {
		this.list = list;
	}

	public String queryProvince() {
		String hql = "from Provinces";
		list = ps.findEntityByHQL(hql);
		return SUCCESS;
	}

	public String getPri() {
		String hql = "from Provinces";
		list = ps.findEntityByHQL(hql);
		return "adminreg";
	}

	public String getUniv() {
		String uhql = "from Univs s where s.pid=?";
		Ulist = us.findEntityByHQL(uhql, pid);
		return "getUnivSuccess";
	}

	public String getIndexPri() {
		String hql = "from Provinces";
		list = ps.findEntityByHQL(hql);
		return "getindexPri";
	}

	public String getIndexUniv() {
		String uhql = "from Univs s where s.pid=?";
		Ulist = us.findEntityByHQL(uhql, pid);
		return "getindexUniv";
	}

	public void codeConfirm() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String cod = (String) request.getSession().getAttribute("authCode");
		// System.out.println(cod+":"+code);
		if (cod.toLowerCase().equals(code.toLowerCase())) {
			out.print("1");
		} else {
			out.print("0");
		}
		request.getSession().invalidate();
		out.flush();
		out.close();

	}

	/**
	 * email验证
	 */
	public void emailConfirm() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String hql = "from Admin a where a.email=?";
		List<Admin> l = as.findEntityByHQL(hql, email);

		if (l.size() == 0) {
			out.print("1");
		} else {
			out.print("0");
		}

		out.flush();
		out.close();

	}

	public String addPicture() {
		if (ValidateUtil.isValid(pictureFileName)) {
			String dir = sc.getRealPath("/upload");
			long l = System.nanoTime();
			String ext = pictureFileName.substring(pictureFileName
					.lastIndexOf("."));
			File newFile = new File(dir, l + ext);
			picture.renameTo(newFile);
			ad.setPicturePath("/upload/" + l + ext);
			ad.setPid(pid);
			ad.setUid(uid);
			ad.setPwd(MD5.GetMD5Code(ad.getPwd()));
			ad.setEmail(email);
			as.saveEntity(ad);
			return SUCCESS;
		} else
			return "regWrong";

	}

	/**
	 * 管理员登陆
	 * 
	 * @return
	 */
	public String adminLogin() {
		String hql = "from Admin a where a.email=? and a.pwd=? and a.confirm=1";
		List<Admin> L = as.findEntityByHQL(hql, email,
				MD5.GetMD5Code(ad.getPwd()));
		if (!L.isEmpty()) {
			// Cookie myCookie=new Cookie("uid",String.valueOf(uid));
			// Cookie myCookie2=new Cookie("pid",String.valueOf(pid));
			// ServletActionContext.getResponse().addCookie(myCookie);
			// ServletActionContext.getResponse().addCookie(myCookie2);
			request.getSession().setAttribute("admin", L.get(0));
			return SUCCESS;
		} else {
			return "adminLoginWrong";
		}
	}
	public String admin(){
		return SUCCESS;
	}

	/**
	 * 修改管理员资料
	 * 
	 * @return
	 */
	public String editAdminById() {
		ad = as.getEntity(ad.getId());
		if (ad!=null) {
			return SUCCESS;
		}
		return "wrong";
	}
	public String editAdmin(){
		ad.setPwd(MD5.GetMD5Code(ad.getPwd()));
		as.updateEntity(ad);
		as.getEntity(ad.getId());
		request.getSession().setAttribute("admin", ad);
		return SUCCESS;
	}
	public void setServletContext(ServletContext arg0) {
		this.sc = arg0;
	}

}
